#! /bin/bash

# Auto Linux Kernel Maker; Automatically un-tars, compiles, and installs a kernel
# Copyright 2009 Anthony Byrne
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


# Make sure your in the folder where a .tar.bz2 downloaded from kernel.org is
# MAKE SURE YOUR ROOT WHEN RUNNING THIS!

# Show error if not root
( #Start LOG
if [ $USER != root ]; then
  zenity --error --text="Must Be Root"
  exit 0
fi

# Warning
zenity --info --text="Make sure that you launched this program from a directory which has a .tar.bz2 downloaded from kernel.org. If you didn't do this right, KILL this program, don't just hit cancel!"

# Ask and fill variables
KERNEL_VER="$(zenity --entry --text="What is the version of the kernel you are trying to install? (e.g 2.6.0) MAKE SURE THIS IS RIGHT!")"

# Decompress
tar xivf linux-${KERNEL_VER}.tar.bz2 | zenity --progress --auto-close --pulsate --text="Decompressing Kernel"

# Go into Kernel source
cd linux-$KERNEL_VER

# Choose a Method
METHOD="$(zenity --height=200 --width=410 --title "Configuration" --text "Choose a method for configuration" --list --radiolist --column "Select" --column "Method" True "Configure using the GUI (requires qt3-examples)" False "Configure using the Terminal" False "Use a pre-existing config file")"

if [[ "$METHOD" == "Configure using the GUI (requires qtexamples)" ]]; then
make xconfig  
fi

if [[ "$METHOD" = "Configure using the Terminal" ]]; then
xterm -e make config  
fi

if [[ "$METHOD" = "Use a pre-existing config file" ]]; then
zenity --info --text="You've set this script to use a preconfigured kernel config file. Put that config file in the linux-$KERNEL_VER directory that was just created. Make sure that file is named .config"
xterm -e make oldconfig  
fi

(
# Compile the kernel (takes a long time)
echo "5"
make 
echo "50"

# Install the kernel modules
echo "# Installing Kernel Modules"
make modules_install 
echo "76"

# Install the kernel
echo "# Installing Kernel"
make install 
echo "87"

# Crestes a new Initial RAM File System (initramfs)
echo "# Running Post-Installation Tasks (Generating INITRAMFS)" 
update-initramfs -k ${KERNEL_VER} -c
echo "95"

# Updates Bootloader
echo "# Running Post-Installation Tasks (Updating GRUB)"
update-grub
echo "100"
) | 
zenity --progress \
          --title="Kernel Compiler" \
          --text="Compiling Kernel (This takes a really long time!)" \
          --auto-close \
          --percentage=0

cd ..

# Clean up process
DELETE="$(zenity --height=200 --width=410 --title "Clean up?" --text "Do you want to clean up?" --list --radiolist --column "Select" --column "Method" True "No, Leave everything." False "Yes, just delete the un-needed files for me.")"

if [[ "$DELETE" == "Yes, just delete the un-needed files for me." ]]; then
rm -r -f linux-$KERNEL_VER
zenity --info --text="If you want to use the config file from this kernel in the furture, it is located in /boot/config-${KERNEL_VER}"
fi

# Doesn't work. May be fixed in next version
#if [[ "$DELETE" == "Yes, but keep the .config file for the future." ]]; then
#cp ./linux-${KERNEL_VER}/.config
#mv ./.config ./dotconfig
#rm -r -f linux-$KERNEL_VER
#fi

# ALL DONE
zenity --info --text="All Done! Your new kernel has been installed!"
) 2>&1 | tee AKM.log
